package com.lc.YYGH.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lc.YYGH.hosp.repository.LawerRepository;
import com.lc.YYGH.hosp.service.LawerService;
import com.lc.YYGH.model.hosp.Department;
import com.lc.YYGH.model.office.LawOffice;
import com.lc.YYGH.model.office.Lawer;
import com.lc.YYGH.vo.office.LawerQueryVo;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class LawerServiceImpl implements LawerService {
    @Autowired
    private LawerRepository lawerRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        Lawer lawer= JSONObject.parseObject(JSONObject.toJSONString(paramMap),Lawer.class);
        //从数据库中查到了科室
        Lawer lawer1=lawerRepository.findByOfficecodeAndLawercode(lawer.getOfficecode(),lawer.getLawercode());

        if(null!=lawer1)
        {
            //科室已经存在 更新
            lawer1.setUpdateTime(new Date());
            lawer1.setIsDeleted(0);
            lawerRepository.save(lawer1);

        }
        else
        {
            Date now=new Date();
            lawer.setCreateTime(now);
            lawer.setUpdateTime(now);
            lawer.setIsDeleted(0);
            lawerRepository.save(lawer);
        }

    }

    @Override
    public Page<Lawer> findPageLawer(int page, int limit, LawerQueryVo lawerQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
       // Department department = new Department();
        Lawer lawer=new Lawer();
        //将Vo对象转为department
        BeanUtils.copyProperties(lawerQueryVo,lawer);
        lawer.setIsDeleted(0);
        //构造条件查询规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Lawer> example = Example.of(lawer,matcher);


        Page<Lawer> all = lawerRepository.findAll(example,pageable);
        System.out.println(all.toString());
        return all;
    }

    @Override
    public void removeLawer(String lawercode,String officecode) {
        Lawer lawer=lawerRepository.findByOfficecodeAndLawercode(officecode,lawercode);
        if(null!=lawer)
        {
            lawerRepository.deleteById(lawer.getId());
        }
    }

    @Override
    public Page<Lawer> getLawerList(int page, int limit, String officecode) {
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        // Department department = new Department();
        Lawer lawer=new Lawer();
        lawer.setOfficecode(officecode);
        //构造条件查询规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Lawer> example = Example.of(lawer,matcher);


        Page<Lawer> all = lawerRepository.findAll(example,pageable);
        System.out.println(all);
        return all;
    }

    @Override
    public Map<String, Object> getDetail(String id) {
        System.out.println(""+id);
        Lawer lawer=lawerRepository.findById(id).get();
//        BookingRule bookingRule=hospital.getBookingRule();
        //创建map存储数据
        Map<String,Object> map=new HashMap<>();
        map.put("lawer",lawer);
        //  map.put("bookingRule",bookingRule);

        //不需要重复返回
        // hospital.setBookingRule(null);
        return map;
    }



}
